/**
 *******************************************************************************
 * @file  ev_hc32f4a0_lqfp176_wm8731.c
 * @brief This file provides configure functions for wm8731 of the board
 *        EV_F4A0_LQ176_V10.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-12       yangjp          First version
   2020-10-30       Zhangxl         Use new I2C API
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "ev_hc32f4a0_lqfp176_wm8731.h"

/**
 * @addtogroup BSP
 * @{
 */

/**
 * @addtogroup EV_HC32F4A0_LQFP176
 * @{
 */

/** @defgroup EV_HC32F4A0_LQFP176_WM8731 HC32F4A0 EVB LQFP176 WM8731
 * @{
 */

#if ((BSP_ON == BSP_WM8731_ENABLE) && \
     (BSP_EV_HC32F4A0_LQFP176 == BSP_EV_HC32F4A0))

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @defgroup EV_HC32F4A0_LQFP176_WM8731_Global_Functions HC32F4A0 LQFP176 EVB WM8731 Global Functions
 * @{
 */

/**
 * @brief  WM8731 I2C initialize
 * @param  None
 * @retval None
 */
void BSP_I2C_WM8731_Init(void)
{
    float32_t fErr;
    stc_i2c_init_t stcI2cInit;
    stc_gpio_init_t stcGpioInit;

    /* Configuration I2C GPIO */
    (void)GPIO_StructInit(&stcGpioInit);
    (void)GPIO_Init(WM8731_I2C_SCL_PORT, WM8731_I2C_SCL_PIN, &stcGpioInit);
    (void)GPIO_Init(WM8731_I2C_SDA_PORT, WM8731_I2C_SDA_PIN, &stcGpioInit);
    GPIO_SetFunc(WM8731_I2C_SCL_PORT, WM8731_I2C_SCL_PIN, WM8731_I2C_SCL_FUNC, PIN_SUBFUNC_DISABLE);
    GPIO_SetFunc(WM8731_I2C_SDA_PORT, WM8731_I2C_SDA_PIN, WM8731_I2C_SDA_FUNC, PIN_SUBFUNC_DISABLE);

    /* I2C1 clock enable */
    PWC_Fcg1PeriphClockCmd(WM8731_I2C_UNIT_CLK, Enable);

    I2C_DeInit(WM8731_I2C_UNIT);
    /* Init I2C structure */
    (void)I2C_StructInit(&stcI2cInit);
    stcI2cInit.u32Baudrate  = WM8731_I2C_BAUDRATE;
    stcI2cInit.u32SclTime   = 0U;
    stcI2cInit.u32ClkDiv    = I2C_CLK_DIV16;
    (void)I2C_Init(WM8731_I2C_UNIT, &stcI2cInit, &fErr);
    /* Enable I2C */
    I2C_Cmd(WM8731_I2C_UNIT, Enable);
}

/**
 * @brief  WM8731 I2C register write
 * @param  [in] u8Reg                   WM8731 register
 * @param  [in] u16Value                Register value
 * @retval None
 */
void BSP_I2C_WM8731_Write(uint8_t u8Reg, uint16_t u16Value)
{
    uint8_t u8WriteData[2];

    u8WriteData[0] = (u8Reg & 0xFEU) | (uint8_t)((u16Value >> 8U) & 0x1U);
    u8WriteData[1] = (uint8_t)(u16Value & 0xFFU);

    (void)I2C_Start(WM8731_I2C_UNIT, WM8731_I2C_TIMEOUT);
    (void)I2C_TransAddr(WM8731_I2C_UNIT, WM8731_DEV_ADDR, WM8731_DEV_WRITE, WM8731_I2C_TIMEOUT);
    (void)I2C_TransData(WM8731_I2C_UNIT, u8WriteData, 2UL, (WM8731_I2C_TIMEOUT << 1U));
    (void)I2C_Stop(WM8731_I2C_UNIT, WM8731_I2C_TIMEOUT);
}

/**
 * @brief  WM8731 I2C Delay
 * @param  [in] u32Delay                ms
 * @retval None
 */
void BSP_I2C_WM8731_Delay(uint32_t u32Delay)
{
    DDL_DelayMS(u32Delay);
}

/**
 * @}
 */

#endif /* BSP_EV_HC32F4A0_LQFP176/BSP_WM8731_ENABLE */

/**
 * @}
 */

/**
 * @}
 */

/**
* @}
*/

/******************************************************************************
 * EOF (not truncated)
 *****************************************************************************/
